package phoneinfo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class SortNumbers {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		if (args.length!=2) {
			System.err.println("path err!!");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"jj");
		job.setJarByClass(SortNumbers.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.setMapperClass(soMaps.class);
		job.setReducerClass(soReduces.class);
		
		job.setOutputKeyClass(SortNum.class);
		job.setOutputValueClass(NullWritable.class);
		job.waitForCompletion(true);
		
	}
	public static class soMaps extends Mapper<LongWritable,Text,SortNum,NullWritable>{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,SortNum,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			int frist=Integer.parseInt(lines[0]);
			int second = Integer.parseInt(lines[1]);
			SortNum sortNum = new SortNum(frist,second);
			context.write(sortNum, NullWritable.get());
		};
	}
	//in this map ,we let sortNum as a 'key' output.
	//in shuffle ,becasuse sortNum hava do WritableCompareable[change the sort rulls],so,in this shuffle,the program will
	//do as this "sortNum".
	public static class soReduces extends Reducer<SortNum,NullWritable,SortNum,NullWritable>{
		protected void reduce(SortNum k2, java.lang.Iterable<NullWritable> values, org.apache.hadoop.mapreduce.Reducer<SortNum,NullWritable,SortNum,NullWritable>.Context context) throws java.io.IOException ,InterruptedException {
			context.write(k2, NullWritable.get());
		};
	}
}
